<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	选择优惠券modal，通过访问此页面选择获取指定商品id
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
	//优惠券列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=$params=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0);
	$fields=array('title');
	if($_RQ['find']){
		$field=in_array($_RQ['field'],$fields)?$_RQ['field']:'title';
		$params[$field.' like']='%'.$_RQ['find'].'%';
		$return['params']['find']=$_RQ['find'];
		$return['params']['field']=$field;
	}
	if($_RQ['nomerch']){
		$params['merchid']=0;
	}
	if($_RQ['isall']){
		unset($params['merchid']);
		$return['params']['isall']=1;
	}
	if($_RQ['coupontype']){
		$params['coupontype']=$_RQ['coupontype'];
		$return['params']['coupontype']=$_RQ['coupontype'];
	}
	$list=pdo_getslice('jy_weishop_coupon',$params,array($pindex,$psize),$total,array('id','coupontype','title','last','merchid','limitmerchids','ismerch','merchtype'),'','id desc');
	if($list){
		if($this->plugin_exists('jy_merch')){
			$jy_merch=true;
		}
		foreach($list as &$item){
			if($_RQ['op']=='many'){
				$item['res']=array(
					'title'=>$item['title'],
					'id'=>$item['id'],
				);
			}else{
				$item['res']=$item['id'];
			}
			if($item['merchid'] && $jy_merch){
				$item['merchtitle']='['.$item['merchid'].']';
				$merchname=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
				$merchname=$merchname?:'商户已删除';
				$item['merchtitle'] .=$merchname;
			}elseif(!$item['merchtype'] && $item['ismerch']==1){
				$item['merchtitle']='全商户优惠券';
			}elseif($item['merchtype'] && $item['ismerch']==1 && $item['limitmerchids']){
				$limitmerchidskey=str_array($item['limitmerchids']);
				if($limitmerchidskey){
					$merchname=pdo_getcolumn('jy_merch_user',array('id'=>$limitmerchidskey,'uniacid'=>$_SESSION['uniacid']),'merchname');
				}
				$item['merchtitle']=$merchname?:'平台优惠券';
			}else{
				$item['merchtitle']='平台优惠券';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
			'type'=>'list',
			'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
				array('name'=>'id','title'=>'优惠券id','type'=>'string'),
				array('name'=>'title','title'=>'优惠券名称','type'=>'string'),
				array('name'=>'coupontype','title'=>'类型','type'=>'radio','options'=>'coupontype'),
				array('name'=>'merchtitle','title'=>'所属商户','type'=>'string'),
			),'action'=>array(
				array('type'=>'modal_result','name'=>'res','title_field'=>'title','title'=>'选取'),
			)),
			'search_one'=>array('name'=>'find','select_name'=>'field','select_title'=>'筛选','is_radio'=>true,'options'=>'fields'),
	);
	$return['coupontype']=array('1'=>'购物优惠券','2'=>'充值优惠券','3'=>'礼品券');
	$return['fields']=array(
		'title'=>'优惠券名称',
	);
	exi($return);

?>